/**
 * 
 */
package com.hj.kim.Util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrator
 * 
 */
public class HttpResponse {
	public static final String RESPONSE_MESSAGE_SUCCESS = "success";
	public static final String RESPONSE_MESSAGE_CANCELED = "canceled";
	public static final String RESPONSE_MESSAGE_FAILED = "failed";
	public static final String RESPONSE_MESSAGE_ERROR = "error";
	
	public static final int BUFFER_SIZE = 1024 * 8;

	public static void sendMessage(HttpServletResponse response, String text) {
		try {
			response.setContentType("text/html;charset=UTF-8");
			response.getWriter().print(text);
			response.flushBuffer();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendXML(HttpServletResponse response, String text) {
		try {
			response.setContentType("text/xml;charset=UTF-8");
			response.getWriter().print(text);
			response.flushBuffer();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendImage(HttpServletResponse response, ByteArrayOutputStream out) {		
		try {			
			response.setContentType("image/png");
//			response.setHeader("Content-Disposition", "attachment; filename=exportMap");
//			response.setHeader("Content-Description", "JSP Generated Data");
			response.setContentLength((int)out.size());		
			
			out.writeTo(response.getOutputStream());
			out.flush();			
				    
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void sendExcelFile(HttpServletResponse response, File file) {		
		try {			
			FileInputStream fis = new FileInputStream(file);
			ServletOutputStream sos = response.getOutputStream();
			
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Transfer-Encoding", "binary");
			response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(file.getName(), "utf-8"));
			response.setHeader("Content-Description", "JSP Generated Data");
			response.setContentLength((int)file.length());
			
		    byte[] bytes = new byte[BUFFER_SIZE];
		    
		    while (fis.read(bytes) > 0) {
		        sos.write(bytes);
		        sos.flush();
		    }
		    
		    fis.close();
		    
		    file.delete();
		    	    
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void sendExcelFile(HttpServletResponse response, File file, String name) {		
		try {			
			FileInputStream fis = new FileInputStream(file);
			ServletOutputStream sos = response.getOutputStream();
			
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Transfer-Encoding", "binary");
			response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(name, "utf-8"));
			response.setHeader("Content-Description", "JSP Generated Data");
			response.setContentLength((int)file.length());
			
		    byte[] bytes = new byte[BUFFER_SIZE];
		    
		    while (fis.read(bytes) > 0) {
		        sos.write(bytes);
		        sos.flush();
		    }
		    
		    fis.close();
		    
		    file.delete();
		    	    
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void sendExcelFile(HttpServletResponse response, String fileName, String name) {		
		try {			
			File file = new File(fileName);
			FileInputStream fis = new FileInputStream(file);
			ServletOutputStream sos = response.getOutputStream();
			
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Transfer-Encoding", "binary");
			response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(name, "utf-8"));
			response.setHeader("Content-Description", "JSP Generated Data");
			response.setContentLength((int)file.length());
			
		    byte[] bytes = new byte[BUFFER_SIZE];
		    
		    while (fis.read(bytes) > 0) {
		        sos.write(bytes);
		        sos.flush();
		    }
		    
		    fis.close();		    	    
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void sendZipFile(HttpServletResponse response, File file) {		
		try {			
			FileInputStream fis = new FileInputStream(file);
			ServletOutputStream sos = response.getOutputStream();
			
			response.setContentType("application/zip");
			response.setHeader("Content-Transfer-Encoding", "binary");
			response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
			response.setHeader("Content-Description", "JSP Generated Data");
			response.setContentLength((int)file.length());
			
		    byte[] bytes = new byte[BUFFER_SIZE];
		    
		    while (fis.read(bytes) > 0) {
		        sos.write(bytes);
		        sos.flush();
		    }
		    
		    fis.close();
		    	    
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}